package com.ly.algorithm.leetcode.tree;

/**
 * @Classname Problem226
 * @Description
 *
 * 输入：
 *
 *      4
 *    /   \
 *   2     7
 *  / \   / \
 * 1   3 6   9
 * 输出：
 *
 *      4
 *    /   \
 *   7     2
 *  / \   / \
 * 9   6 3   1
 *
 *
 *
 * @Date 2020/12/22 14:29
 * @Author 冷心影翼
 */
public class Problem226 {

	public static void main(String[] args) {
		Solution226 solution226 = new Solution226();
		TreeNode treeNode = new TreeNode(4);
		treeNode.left = new TreeNode(2);
		treeNode.left.left = new TreeNode(1);
		treeNode.left.right = new TreeNode(3);
		treeNode.right = new TreeNode(7);
		treeNode.right.left = new TreeNode(6);
//		treeNode.right.right = new TreeNode(9);
		TreeNode res = solution226.invertTree(treeNode);
		System.out.println(res.left.left);
	}

}


class Solution226 {
	public TreeNode invertTree(TreeNode root) {
		reverse(root);
		return root;
	}

	public void reverse(TreeNode root) {
		if(root==null) {
			return;
		}
		TreeNode node = root.left;
		root.left =  root.right;
		root.right = node;
		if(root.left!=null)
			reverse(root.left);
		if(root.right!=null)
			reverse(root.right);

	}
}